#!/usr/bin/bash
set -x

os_version=$1
k8s_version=$2
runtime=$3

if [[ "$os_version" == "" ]];then
	echo "please input correct exist os version !"
	exit 1
fi
if [[ "$k8s_version" == "" ]];then
	echo "please input correct exist k8s version !"
	exit 1
fi
if [[ "$runtime" == "" ]];then
	echo "please input correct exist runtime !"
	exit 1
fi

img_list=(hb.sincerecloud.com/k8s/v1.25.11/pause:3.8 hb.sincerecloud.com/k8s/v1.25.11/kube-keepalived:v3.10 hb.sincerecloud.com/k8s/v1.25.11/coredns:v1.9.3 hb.sincerecloud.com/k8s/v1.25.11/etcd:3.5.6-0 hb.sincerecloud.com/k8s/v1.25.11/kube-apiserver:v1.25.11 hb.sincerecloud.com/k8s/v1.25.11/kube-scheduler:v1.25.11 hb.sincerecloud.com/k8s/v1.25.11/kube-controller-manager:v1.25.11 hb.sincerecloud.com/k8s/v1.25.11/kube-proxy:v1.25.11 hb.sincerecloud.com/calico/v3.24.1/node:v3.24.1 hb.sincerecloud.com/calico/v3.24.1/cni:v3.24.1 hb.sincerecloud.com/calico/v3.24.1/kube-controllers:v3.24.1 hb.sincerecloud.com/calico/v3.24.1/typha:v3.24.1 hb.sincerecloud.com/kubernetesui/metrics-scraper:v1.0.8 hb.sincerecloud.com/kubernetesui/dashboard:v2.7.0)

if [[ "$os_version" =~ "centos" ]]; then
    install_cmd="yum install -y kubelet-${k8s_version}-0 kubectl-${k8s_version}-0 cri-tools-1.26.0-0"
    del_cri_cmd="systemctl stop"
    del_rep_cmd="rm -f /etc/yum.repos.d/kubernetes.repo"
    lock_version_cmd="yum versionlock cri-tools kubelet kubectl"
elif [[ "$os_version" =~ "ubuntu" ]]; then
    install_cmd="apt-get install -y kubelet-${k8s_version}-0 kubectl-${k8s_version}-0 cri-tools=1.26.0-00"
    del_cri_cmd="apt-get autoremove -y"
    del_rep_cmd="rm -f /etc/apt/sources.list.d/kubernetes.list*"
    del_pkg_cmd="rm -f /var/cache/apt/archives/*.deb"
    lock_version_cmd="apt-mark hold kubelet kubectl cri-tools"
fi

if [[ "$runtime" == "containerd" ]]; then
    cri_conf="/etc/containerd/config.toml"
    cri_socket="unix:///var/run/containerd/containerd.sock"
    del_cri_cmd="${del_cri_cmd} cri-o"
    disable_cri="cri-o"
elif [[ "$runtime" == "cri-o" ]]; then
    cri_conf="/etc/crio/crio.conf"
    cri_socket="unix:///var/run/crio/crio.sock"
    del_cri_cmd="${del_cri_cmd} containerd"
    disable_cri="containerd"
fi

#安装kubelet kubectl cri-tools 同时卸载另一个运行时
sshpass -p Hao.1234 ssh -o StrictHostKeyChecking=no root@10.0.1.234 systemctl disable ${disable_cri}.service
sshpass -p Hao.1234 ssh root@10.0.1.234 "$install_cmd && $lock_version_cmd && $del_cri_cmd"
sleep 8

for i in ${img_list[@]} ; do
    #拉取镜像
    sshpass -p Hao.1234 ssh root@10.0.1.234 crictl -r $cri_socket pull $i
    #修改容器运行时pause镜像
    if [[ $i =~ "pause" ]]; then
        sshpass -p Hao.1234 ssh root@10.0.1.234 sed -i "s,#PAUSE_IMAGE#,${i},g" $cri_conf
    fi
done

#下载agent
sshpass -p Hao.1234 ssh root@10.0.1.234 systemctl stop voneyun_agent.service
sshpass -p Hao.1234 ssh root@10.0.1.234 curl -o /usr/bin/voneyun_agent 10.50.29.107/getAgent/$k8s_version
sshpass -p Hao.1234 ssh root@10.0.1.234 journalctl --vacuum-time=10s
sshpass -p Hao.1234 ssh root@10.0.1.234 systemctl start voneyun_agent.service
sshpass -p Hao.1234 ssh root@10.0.1.234 journalctl -n15 -u voneyun_agent.service | head -n 15
sshpass -p Hao.1234 ssh root@10.0.1.234 systemctl restart ${runtime}.service

#清理数据
sshpass -p Hao.1234 ssh root@10.0.1.234 "sed -i  's,10.50.9.216    hb.sincerecloud.com,,g' /etc/hosts"
sshpass -p Hao.1234 ssh root@10.0.1.234 history -c
sshpass -p Hao.1234 ssh root@10.0.1.234 rm -f ~/.bash_history
sshpass -p Hao.1234 ssh root@10.0.1.234 $del_rep_cmd
if [ -n "$del_pkg_cmd" ]; then
    sshpass -p Hao.1234 ssh root@10.0.1.234 $del_pkg_cmd
fi